package com.demo2do.fms.admin.web.controller.authority.role;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.authority.ResourceService;
import com.demo2do.fms.entity.authority.Resource;
import com.demo2do.fms.entity.authority.RoleTemplate;

/**
 * 角色模板管理
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/role-templates")
@MenuSetting("menu-authority")
public class RoleTemplateController {

    @Autowired
    private ResourceService resourceService;
    
    /**
     * list
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-role-template-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("authority/role/role-template-list");
        
        modelAndView.addObject("roleTemplates", RoleTemplate.values());
        
        return modelAndView;
        
    }
    
    /**
     * view
     * 
     * @param id
     * @param page
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-role-template-management")
    public ModelAndView view(@PathVariable("id") int id, Page page){
        
        ModelAndView modelAndView = new ModelAndView("authority/role/role-template-view");
        
        // 1. get role_template by template_value
        RoleTemplate roleTemplate = RoleTemplate.ordinalOf(id);
        modelAndView.addObject("roleTemplate", roleTemplate);
        
        // 2. count resources by role_template
        int totals = resourceService.count(roleTemplate);
        if(totals > 0){
            
            // 3. list resources by role_template and page
            List<Resource> resources = resourceService.list(roleTemplate, page.with(totals));
            
            modelAndView.addObject("resources", resources);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
}
